package com.zegome.game.gomestory.main.scene.draw;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.TweenCallback;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.zegome.facebook.IFacebookBridge;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.gomestory.main.helper.IMAction;
import com.zegome.game.gomestory.main.scene.BaseGameScreen;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.screen.GameScreenOption;

public class Draw2Screen extends BaseGameScreen{
	
	public Draw2Screen(AndroidGameController controller, String name, final IFacebookBridge bridge) {
		super(controller, name,bridge);		
		setConfig(true, false, true,true);		
		initData();
	}
	
	private ImageButton  save, cancel;	
	
	private ImageButton draw, eraser;
	private Image bgDraw, bgEraser;
	private Image drawIcon, eraserIcon;
	
	Label level;
	private Table iconTable;
	private ScrollPane iconScroll;

	Sprite painter;
	private Sprite bg;
	private Sprite paper;
	private Sprite[][] square; 
	private Sprite[][] icon; 
	private float xBegin, yBegin;
	private float wiPikachu, hiPikachu;
	private int maxCol, maxRow;
	private int modeCurrent = 0;
	private int currentChapter;
	// 0: Draw
	// 1: Eraser
	
	int indexHelp = 0;
	
	public void initData(){
		//if(square == null)
		square = new Sprite[maxRow][maxCol];
		//if(icon == null)
		icon = new Sprite[maxRow][maxCol];
		//if(touch == null)
		touch = new Vector3();
	}
	public void show() {
		ctr.a.soundOnOff();
		indexHelp = 0;
		ctr.a.isHasDialog.removeAllElements();
		ctr.d.backLis.removeAllElements();
		state = 0;
		modeCurrent = 0;
		tweenManager.killAll();
		stage.clear();
		createUi();
		super.show();
		ctr.a.bgSong.play();
		
	}

	public void createUi() {		
		
		if(level==null){
			boolean isMax = (ctr.d.levelDraw == 5)?true:false;
			level = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
			level.setText("Level "+(isMax?"Max":ctr.d.levelDraw+""));
			level.setPosition((10), (480-230));					
		}
		if (save == null) {
			save = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.ok,
				   null, null, (739), (480-122));
			save.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.maps.size()>ctr.d.levelDraw+2){
						ctr.stageHelper.createDialog(getStage(), getTween(), ctr.lang.getCannotSaveMoreMap(), 2,
								new ChangeListener() {
							@Override
							public void changed(ChangeEvent event, Actor actor) {
								ctr.stageHelper.createDialogPayGoldDiamond(Draw2Screen.this, stage, tweenManager,500,5000,new IMAction() {
									
									@Override
									public void action() {
										// TODO Auto-generated method stub
										ctr.d.levelDraw++;
										ctr.d.saveAccount();
										ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.getLevelArtist(), 1, null, null);
									}
								});
//								if(ctr.d.money<5000){
//									ctr.androidHelper.showToast(ctr.lang.getNotEnoughMoney(1, 5000));
//									ctr.stageHelper.createDialogMoney(Draw2Screen.this, stage, tweenManager);
//								}
//								else{
//									ctr.d.money -= 5000;
//									ctr.d.levelDraw++;
//									ctr.d.saveAccount();									
//									ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.getLevelArtist(), 1, null, null);
//								}
								}
						}, null );							
						return;
					}
					int pikaNumber=0;
					String map = "";
					
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){				
							if(icon[i][j].isVisible()){
								pikaNumber++;								
								map+="1";
							}
							else{
								map+="0";
							}
						}
					}
					ChangeListener okLis = new ChangeListener() {
						@Override
						public void changed(ChangeEvent event, Actor actor) {													
							for(int i=0;i<maxRow;i++){
								for(int j=0;j<maxCol;j++){														
									square[i][j].setVisible(false);
									icon[i][j].setVisible(false);
								}
							}
							ctr.a.bgSong.pause();
							clear();
							mController.setScreen(mController.getScreen("DrawScreen"));
						}
					};
					if(pikaNumber < 6){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.drawMin3Pair, 1, null, null);
					}
					else{
						if(pikaNumber%2!=0){
							ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.drawEvenNumber, 1, null, null);
						}
						else{							
							ctr.stageHelper.createDialogSaveMap(stage, tweenManager,map,currentChapter,okLis);
						}
					}					
				}
			});
		}
		
		if (cancel == null) {
			cancel = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.cancel,
					null, null, (742), (480-190));
			cancel.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					ctr.stageHelper.createDialog(stage, tweenManager,
						ctr.lang.deleteThisMap, 2,
						new ChangeListener() {
							
							@Override
							public void changed (ChangeEvent event, Actor actor) {
								// TODO Auto-generated method stub
								for(int i=0;i<8;i++){
									for(int j=0;j<12;j++){														
										square[i][j].setVisible(false);
										icon[i][j].setVisible(false);
									}
								}
							}
						}, null);					
				}
			});
		}
		
		createIconScroll();
		
		stage.addActor(iconScroll);
		stage.addActor(draw);
		stage.addActor(eraser);
		stage.addActor(save);
		stage.addActor(level);
		stage.addActor(cancel);
		Table avatar = ctr.stageHelper.createAvatar(this, stage, tweenManager, (0), (480-99), 1f);
		stage.addActor(avatar);
		//
		if(bg==null){
			bg = new Sprite(0, 0, ctr.a.bgDraw);
			bg.setSize((800), (480));
			attachChild(bg);			
		}
		
		addIconGomeSmall();
		
		if(painter==null){
			painter = new Sprite(0, 0, ctr.a.gomePainter);
			painter.setSize((164), (130));
			painter.setPosition(0, (480-223));
			attachChild(painter);			
		}
		xBegin = (140);
		yBegin = (13);
		wiPikachu = (50);
		hiPikachu = (50);
		maxCol = 12;
		maxRow = 8;
		if(ctr.d.isLDPI){
			maxCol = 10;
			maxRow = 6;
			wiPikachu = 60;
			hiPikachu = 66;
		}		
		initData();
		//
		if(paper == null){
			paper = new Sprite(xBegin, yBegin, ctr.a.grid);
			paper.setSize(599, 398);
			//paper.setColor(paper.getColor().r, paper.getColor().g, paper.getColor().b, 0.1f);
			attachChild(paper);
		}	
		//
		//
		currentChapter = 7;
		for(int i=0;i<maxRow;i++){
			for(int j=0;j<maxCol;j++){				
				if(square[i][j]==null){					
					square[i][j] = new Sprite(0, 0, ctr.a.boundSquare);
					square[i][j].setSize((wiPikachu), (hiPikachu));
					square[i][j].setPosition(xBegin+j*(wiPikachu), yBegin+i*(hiPikachu));
					attachChild(square[i][j]);
				}
				if(icon[i][j]==null){					
					icon[i][j] = new Sprite(0, 0, ctr.d.getIconTemplate(7));
					icon[i][j].setSize((wiPikachu), (hiPikachu));
					icon[i][j].setPosition(xBegin+j*(wiPikachu), yBegin+i*(hiPikachu));
					attachChild(icon[i][j]);
				}
				square[i][j].setVisible(false);
				icon[i][j].setVisible(false);
			}
		}
	}
		
	public void createIconScroll(){
		if (draw == null) {
			draw = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.transRegion,
					null, null, (0), 168,70,70);
			
			bgDraw = ctr.stageHelper.createImage(ctr.a.boundSquare, 0, 0,70,70);
			drawIcon = ctr.stageHelper.createImage(ctr.a.brush, 0, 0,70,70);
			bgDraw.setTouchable(Touchable.disabled);
			drawIcon.setTouchable(Touchable.disabled);
			draw.addActor(bgDraw);
			draw.addActor(drawIcon);
			draw.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					modeCurrent = 0;
				}
			});
		}
		if (eraser == null) {
			eraser = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.transRegion,
					null, null, (70), 168,70,70);
			bgEraser = ctr.stageHelper.createImage(ctr.a.boundSquare, 0, 0,70,70);
			eraserIcon = ctr.stageHelper.createImage(ctr.a.eraser, 0, 0,70,70);
			bgEraser.setTouchable(Touchable.disabled);
			eraserIcon.setTouchable(Touchable.disabled);
			eraser.addActor(bgEraser);
			eraser.addActor(eraserIcon);
			eraser.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					modeCurrent = 1;
				}
			});
		}
		if(iconTable == null){
			iconTable = ctr.stageHelper.createTableWithTransBg(ctr.g.mSkin, 0, 0, (140), (4*70));		
			ImageButton noel = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			noel.addActor(ctr.stageHelper.createImage(ctr.d.getIconTemplate(7), 0, 0,70,70));			
			noel.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;		
					currentChapter = 7;	
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){
							icon[i][j].setTextureRegion(ctr.d.getIconTemplate(7));
							icon[i][j].setSize((wiPikachu), (hiPikachu));
						}
					}
				}
			});		
			ImageButton candy = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			candy.addActor(ctr.stageHelper.createImage(ctr.d.getIconTemplate(0), 0, 0,70,70));			
			candy.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;		
					currentChapter = 0;	
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){
							icon[i][j].setTextureRegion(ctr.d.getIconTemplate(0));
							icon[i][j].setSize((wiPikachu), (hiPikachu));
						}
					}
				}
			});		
			ImageButton school = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			Image ic1 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(1), 0, 0,70,70);
			school.addActor(ic1);			
			if(ctr.d.chapters[1].isUnLock == false){
				ic1.setColor(0.5f, 0.5f, 0.5f, 1f);
			}
			school.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[1].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					currentChapter = 1;
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){
							icon[i][j].setTextureRegion(ctr.d.getIconTemplate(1));
							icon[i][j].setSize((wiPikachu), (hiPikachu));
						}
					}
				}
			});		
	
			ImageButton vac = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			Image ic2 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(2), 0, 0,70,70);
			vac.addActor(ic2);			
			if(ctr.d.chapters[2].isUnLock == false){
				ic2.setColor(0.5f, 0.5f, 0.5f, 1f);
			}
			vac.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[2].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					currentChapter = 2;
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){
							icon[i][j].setTextureRegion(ctr.d.getIconTemplate(2));
							icon[i][j].setSize((wiPikachu), (hiPikachu));
						}
					}
				}
			});		
			ImageButton casi = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			Image ic3 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(3), 0, 0,70,70);
			casi.addActor(ic3);			
			if(ctr.d.chapters[3].isUnLock == false){
				ic3.setColor(0.5f, 0.5f, 0.5f, 1f);
			}
			casi.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[3].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					currentChapter = 3;
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){
							icon[i][j].setTextureRegion(ctr.d.getIconTemplate(3));
							icon[i][j].setSize((wiPikachu), (hiPikachu));
						}
					}
				}
			});		
			ImageButton bd = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			Image ic4 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(4), 0, 0,70,70);
			bd.addActor(ic4);			
			if(ctr.d.chapters[4].isUnLock == false){
				ic4.setColor(0.5f, 0.5f, 0.5f, 1f);
			}
			bd.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[4].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, "Chủ đề chưa được mở!", 1, null, null);
						return;
					}
					currentChapter = 4;
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){
							icon[i][j].setTextureRegion(ctr.d.getIconTemplate(4));
							icon[i][j].setSize((wiPikachu), (hiPikachu));
						}
					}
				}
			});		
			ImageButton ot = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			Image ic5 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(5), 0, 0,70,70);
			ot.addActor(ic5);			
			if(ctr.d.chapters[5].isUnLock == false){
				ic5.setColor(0.5f, 0.5f, 0.5f, 1f);
			}
			ot.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[5].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					currentChapter = 5;
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){
							icon[i][j].setTextureRegion(ctr.d.getIconTemplate(5));
							icon[i][j].setSize((wiPikachu), (hiPikachu));
						}
					}
				}
			});			
			ImageButton love = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.boundSquare, null, null, 0, 0,70,70);
			Image ic6 = ctr.stageHelper.createImage(ctr.d.getIconTemplate(6), 0, 0,70,70);
			love.addActor(ic6);			
			if(ctr.d.chapters[6].isUnLock == false){
				ic6.setColor(0.5f, 0.5f, 0.5f, 1f);
			}
			love.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.chapters[6].isUnLock == false){
						ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.chapterNotOpen, 1, null, null);
						return;
					}
					currentChapter = 6;
					for(int i=0;i<maxRow;i++){
						for(int j=0;j<maxCol;j++){
							icon[i][j].setTextureRegion(ctr.d.getIconTemplate(6));
							icon[i][j].setSize((wiPikachu), (hiPikachu));
						}
					}
				}
			});		
			iconTable.row();
			iconTable.add(noel);
			iconTable.add(candy);
			iconTable.row();
			iconTable.add(school);
			iconTable.add(vac);
			iconTable.row();
			iconTable.add(casi);
			iconTable.add(bd);
			iconTable.row();
			iconTable.add(ot);
			iconTable.add(love);
		}		
		if(iconScroll == null){
			iconScroll = new ScrollPane(iconTable);
			iconScroll.setBounds((0), (480-402+70-30-35-35+15), (140), (1.5f*70));
			iconScroll.setScrollingDisabled(true, false);
		}
	}
	
	
	Vector3 touch;
	
	void configPosition(final Vector3 vector, final float x, final float y) {
		vector.set(x, y, 0);
		getCamera().unproject(vector);
	}
	
	public void onManagedUpdate(float pDeltaTime){
		
		if(modeCurrent == 0){
			drawIcon.setColor(1f, 1f, 1f, 1f);
			bgDraw.setColor(1f, 1f, 1f, 1f);
			eraserIcon.setColor(0.5f,0.5f,0.5f,1f);
			eraserIcon.setColor(0.5f,0.5f,0.5f,1f);
		}
		else{
			eraserIcon.setColor(1f, 1f, 1f, 1f);
			bgEraser.setColor(1f, 1f, 1f, 1f);
			drawIcon.setColor(0.5f,0.5f,0.5f,1f);
			bgDraw.setColor(0.5f,0.5f,0.5f,1f);
		}
		
		if(ctr.d.isHelpDraw2 && ctr.a.isHasDialog() == false){
			if(indexHelp == 0){
			ctr.stageHelper.createHelpDraw21(stage, tweenManager, new IMAction() {				
				@Override
				public void action() {
					// TODO Auto-generated method stub					
					indexHelp=1;
				}
			});			
			indexHelp = -10;
			}
			else if(indexHelp == 1){
				ctr.stageHelper.createHelpDraw22(stage, tweenManager, new IMAction() {				
					@Override
					public void action() {
						// TODO Auto-generated method stub					
						indexHelp=2;
					}
				});			
				indexHelp = -10;
			}
			else if(indexHelp == 2){
				ctr.stageHelper.createHelpDraw23(stage, tweenManager, new IMAction() {				
					@Override
					public void action() {
						// TODO Auto-generated method stub		
						ctr.d.isHelpDraw2 = false;
						ctr.controller.getActivity().getPreference().saveValue("HelpDraw2", false);
						indexHelp=-10;
					}
				});			
				indexHelp = -10;
			}
		}
		
		update(pDeltaTime);
		float xTouch, yTouch;
		int row, col;
		if (Gdx.input.isTouched()) 
		{			
			configPosition(touch, Gdx.input.getX(), Gdx.input.getY());
			xTouch = touch.x;
			yTouch = touch.y;
			
			if(xTouch>=xBegin 
			&& xTouch <= xBegin+ wiPikachu*maxCol
			&& yTouch >= yBegin
			&& yTouch <= yBegin + hiPikachu*maxRow
					){
			row = (int) ((yTouch - yBegin)/hiPikachu);
			col = (int) ((xTouch - xBegin)/wiPikachu);	
			if(row >= 0 && row<=maxRow-1 && col>=0 && col<=maxCol-1 )
				if(ctr.a.isHasDialog()==false){
				if(modeCurrent == 0){
				if(square[row][col].isVisible()==false){
					square[row][col].setVisible(true);
					icon[row][col].setVisible(true);
				}
				}
				if(modeCurrent == 1){
					if(square[row][col].isVisible()==true){
						square[row][col].setVisible(false);
						icon[row][col].setVisible(false);
					}
				}
				}
			}
		}	
		super.onManagedUpdate(pDeltaTime);
	}
	public void backKeyProcess(){
		int pikaNumber = 0;
		for(int i=0;i<maxRow;i++){
			for(int j=0;j<maxCol;j++){				
				if(icon[i][j].isVisible()){
					pikaNumber++;								
				}
				else{
				}
			}
		}
		if(pikaNumber != 0){
		final ChangeListener okLis = new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {							
				fadeOut(new TweenCallback() {
					@Override
					public void onEvent(int type, BaseTween<?> source) {
						// TODO Auto-generated method stub						
						ctr.a.bgSong.pause();
						clear();
						mController.setScreen(mController.getScreen("DrawScreen"));
					}
				});	
			}
		};
		//postRunnable(new Runnable() {
			
		//	@Override
		//	public void run() {
				// TODO Auto-generated method stub
				ctr.stageHelper.createDialog(stage, tweenManager,ctr.lang.draw2BackToDraw1, 2, okLis, null);
		//	}
		//});
		}
		else{
		//	postRunnable(new Runnable() {
				
		//		@Override
		//		public void run() {
					// TODO Auto-generated method stub
					ctr.a.bgSong.pause();
					clear();
					mController.setScreen(mController.getScreen("DrawScreen"));
		//		}
		//	});
		}
	}
	public void clear(){
		super.clear();
		save = null; cancel = null;	
		draw = null; eraser = null;
		level = null;
		iconTable = null;
		iconScroll = null;

		removeChild(painter);
		removeChild(bg);
		painter = null;
		bg = null;
		paper = null;
		for(int i=0;i<maxRow;i++){
			for(int j=0;j<maxCol;j++){
				if(square[i][i] != null){
				removeChild(square[i][j]);
				removeChild(icon[i][j]);
				square[i][j] = null;
				icon[i][j] = null;
				}
			}
		}
	}
}
